package cn.edu.zhku.citrusmonitor.qa.service;

import java.sql.SQLException;
import java.util.List;

import cn.edu.zhku.citrusmonitor.domain.QA;
import cn.edu.zhku.citrusmonitor.domain.TbAnswer;
import cn.edu.zhku.citrusmonitor.domain.TbQuestion;
import cn.edu.zhku.citrusmonitor.factory.FileDaoFactory;
import cn.edu.zhku.citrusmonitor.qa.dao.QuestionDao;
import cn.edu.zhku.citrusmonitor.qa.dao.QuestionDaoImpl;
import cn.edu.zhku.citrusmonitor.util.PageBean;


public class QuestionServiceImpl implements QuestionService {
	QuestionDao qd=new QuestionDaoImpl();
	@Override
	public void addQ(TbQuestion question) throws SQLException {
		//FileDaoImpl实现类实例  
		QuestionDao qdf= FileDaoFactory.getInstance().createDao(QuestionDao.class);  
		qdf.addQ(question);
	}

	@Override
	public int removeQ(String questionId) throws SQLException {
		return qd.removeQ(questionId);
	}

	@Override
	public int modifyQ(TbQuestion question) throws SQLException {
		return qd.modifyQ(question);
	}

	@Override
	public List<TbQuestion> findQAByWord(String content) throws SQLException {
		return qd.findQAByWord(content);
	}

	@Override
	public List<TbQuestion> showQ(int currentPage, int pageSize,String status) throws SQLException{
		

			return qd.showQ(currentPage,pageSize,status);
		
		
	}

	@Override
	public QA showQAByQid(String questionId) throws SQLException {
		TbQuestion question=qd.findQyByQid(questionId);
		TbAnswer answer = qd.findAByQid(questionId);
		QA qa=new QA();
		qa.setAnswer(answer);
		qa.setQuestion(question);
		return qa;
	}

	@Override
	public List<TbQuestion> showQAll(int pageSize) {
		try {
			
			List<TbQuestion> questionlist= qd.showQAll(pageSize);
		
			return questionlist;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public int showQCount(String status) throws SQLException {
		
		return qd.count(status);
	}

	@Override
	public String findUserPasswordById(String userId) throws SQLException {
		
		return qd.findUserPasswordById(userId);
	}

	@Override
	public PageBean<TbQuestion> showAll(int currentPage, int pageSize) throws SQLException {
		
		int count=qd.count();
		int totalPage=(int) Math.ceil(count/pageSize);
		
		List<TbQuestion> questionlist=qd.showAll(currentPage,pageSize);
		PageBean<TbQuestion> pb=new PageBean<TbQuestion>();
		pb.setCount(count);
		pb.setCurrentPage(currentPage);
		pb.setPageSize(pageSize);
		pb.setTotalPage(totalPage);
		pb.setList(questionlist);
		
		return pb;
	}

	@Override
	public int delAll(String[] ids) throws SQLException {
		
		return qd.delAll(ids);
	}
	
	
}
